package com.online.websocketcluster.controller;

import com.alibaba.fastjson.JSONObject;
import com.online.websocketcluster.entity.HbEntity;
import com.online.websocketcluster.entity.WeChatUserInfo;
import com.online.websocketcluster.service.AgentInterfaceService;
import com.online.websocketcluster.service.SessionManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.Session;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.online.websocketcluster.util.HbUtils.HBMAPS;

/**
 * 坐席接口服务
 * @author hsc
 * @create 2018-04-24 17:07
 */
@RestController
public class AgentInterfaceController {
    @Autowired
    private AgentInterfaceService agentInterfaceService;

    @Autowired
    private SessionManageService sessionManageService;

    /**
     * 发送消息通知给坐席接口
     * @return
     */
    @PostMapping(value="/sendMessages")
    public Map sendMessages(@RequestBody Map map) {
        // 获取坐席websocket服务地址
        Map<String, Object> sessionMaps = sessionManageService.getWsAddress(map);
        int wsResult = (int) sessionMaps.get("result");

        // 坐席不在线
        if (wsResult == 1) {
            return  sessionMaps;
        }
        // 存放在redis的applicationName
        String address = (String) sessionMaps.get("address");
        Integer serviceId = (Integer) map.get("service_id");
        Integer sessionId = (Integer) map.get("session_id");
        Integer eId = (Integer) map.get("e_id");
        // 根据id取出系统心跳管理集合的心跳
         HbEntity hbEntity = HBMAPS.get(serviceId);
         if ( null != hbEntity){
             Session session = hbEntity.getSession();
             try {

                 map.remove("service_id");
                 map.remove("e_id");
                 map.remove("session_id");
                 map.remove("address");
                 JSONObject respJson = new JSONObject();
                 JSONObject respHeaderJson = new JSONObject();
                 JSONObject respBodyJson = new JSONObject();
                 // 查询微信用户信息
                 Map weChatUserMap = sessionManageService.getWeChatUser(map);
                 int weChatUserResult = (int) weChatUserMap.get("result");

                // 判断微信用户信息在redis中是否存在(0:不存在1:存在)
                 if (weChatUserResult == 1){
                     WeChatUserInfo info = new WeChatUserInfo();
                     String open_id = (String) weChatUserMap.get("open_id");
                     String name = (String) weChatUserMap.get("name");
                     String weChatAddress = (String) weChatUserMap.get("address");
                     String sex = (String) weChatUserMap.get("sex");
                     int weChatEId = (int) weChatUserMap.get("e_id");
                     info.setAddress(weChatAddress);
                     info.setName(name);
                     info.setSex(sex);
                     info.seteId(weChatEId);
                     info.setOpenId(open_id);
                     respBodyJson.put("session_obj", info);
                 }


                 respHeaderJson.put("cmd", "notify_msg");
                 respHeaderJson.put("e_id", eId);
                 respHeaderJson.put("service_id", serviceId);
                 respBodyJson.put("session_id", sessionId);
                 respJson.put("header", respHeaderJson);
                 respJson.put("body", respBodyJson);
                 // 发送消息通知给坐席
                 session.getBasicRemote().sendText(respJson.toString());
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
        Map<String, Object> maps = new HashMap<>(1);
        maps.put("result", wsResult);
        return maps;
    }
}
